<?php
/**
 * 区块链订单
 */
namespace app\common\model;

use think\Model;
use app\common\model\MemberMatrix as MemberMatrixModel;
use app\api\model\MemberMining as MemberMiningModel;
use app\common\model\UserMoneyLog as UserMoneyLogModel;

class BlockchainOrder extends Model
{
    protected $name = 'blockchain_order';

    /**
     * 添加区块链订单
     * @param $mid  用户ID
     * @param $transaction_id   订单号
     * @param $currency 币种
     * @param $type 支付类型 1: pay_matrix(购买矩阵) 2: pay_mining(购买矿机)
     *  @param $association_id 关联ID
     * @param $address 充值地址
     * @param int $money    支付金额
     * @param int $status   支付状态 默认未支付
     * @return int|string
     */
    public static function add($mid,$txhash,$currency,$type,$association_id,$address='',$money = 0,$status = 1,$returnValuesJoson = ''){
        $order = self::where('txhash',$txhash)->find();
        if(!empty($order)) return 'Transaction order already exists';

        $data = [
            'mid'   =>  $mid,
            'txhash'   =>  $txhash,
            'currency'   =>  $currency,
            'type'   =>  $type,
            'address'   =>  $address,
            'association_id'    =>  $association_id,
            'money' =>  $money,
            'status'   =>  $status,
            'event_data'    =>  $returnValuesJoson,
            'add_time'  =>  time(),
            'update_time'  =>  time(),
        ];

        switch ($type){
            case 'pay_matrix':
                //查询矩阵详情
                $matrix = MemberMatrixModel::getMemberDetails($mid,$association_id);
                if(empty($matrix)) return false;
                //判断是否已支付 是否是链支付 币种是否一样
                if($matrix['status'] == 1 && $matrix['pay_type'] == 2 && $matrix['type'] == $currency) $data['money'] = $matrix['money'];
                break;
        }

        $id = self::insertGetId($data);
        return $id;
    }

    /**
     * 查询链充值列表
     * @param $where    查询条件
     * @param int $page 页码
     * @param int $limit    分页
     * @param string $order 排序
     * @return array
     * @throws \think\db\exception\DbException
     */
    public static function getList($where,$page = 1,$limit = 20,$order='id desc'){
        $list = $data = self::where($where)->order($order)->paginate(['list_rows'=>$limit,'page'=>$page])->toArray();
        return $list;
    }

    /**
     * 链上充值 - 确认订单支付成功
     * @param $order    订单信息
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getStatus($order,$blockchain){
        
        if($blockchain['money'] == $order['money']){
        
            switch ($order['type']){
                case 'pay_matrix':  //执行矩阵已购买
                    MemberMatrixModel::update_paid($order['mid'],$order['association_id']);
                    break;
                case 'pay_arithmetical':
                    $aa =  '购买算力矿机';
                    break;
                case 'recharge':
                    $aa =  '充值';
                    break;

            }
            return true;
        }
        
        return false;
        
    }
    
    public static function upstatus($id,$address){
        self::where('id',$id)->update(['status'=>2,'address'=>$address,'update_time'=>time()]);
    }

    /**
     * 查询指定订单
     * @param $mid 用户ID
     * @param $type 类型
     * @param $association_id   关联ID
     * @return BlockchainOrder|array|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function appoint_info($mid,$type,$association_id){
        $data = self::where(['mid'=>$mid,'type'=>$type,'association_id'=>$association_id])->select();
        return $data;
    }

}